package com.frxuexi.user.repository

import com.frxuexi.lib_base.room.AskDao
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.lib_base.room.User
import com.frxuexi.lib_base.room.UserDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext

import javax.inject.Inject

@OptIn
class UserRepository @Inject constructor(private val userDao: UserDao,private val askDao: AskDao) {
    fun register(user: User): Flow<Long> {
        return flow { emit(userDao.register(user)) }
    }

    fun login(uname:String,upass:String,role:String):Flow<User>{
        return flow { emit(userDao.login(uname, upass, role)) }
    }

    fun addAsk(askLeave: AskLeave):Flow<Long>{
        return flow { emit(askDao.addAsk(askLeave)) }
    }

    suspend fun selectByPage(page:Int,size:Int):List<AskLeave>{
        return withContext(Dispatchers.IO){
            askDao.selectByPage(page, size)
        }
    }

    suspend fun getCount():Int{
        return withContext(Dispatchers.IO){
            askDao.getCount()
        }
    }
}